package com.xyy.kingdeek3cloud.helper;

import com.alibaba.fastjson.JSON;
import com.kingdee.bos.webapi.entity.QueryParam;
import com.kingdee.bos.webapi.sdk.K3CloudApi;
import com.xyy.kingdeek3cloud.entity.erp.OrgGoods;
import com.xyy.kingdeek3cloud.entity.pop.PopErpSkuStockV2Dto;
import com.xyy.kingdeek3cloud.enums.K3FromEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Slf4j
public class JdHelper {
    //查询产地
    public static List<OrgGoods> QueryOrigin(String number, String ckId) {
        K3CloudApi k3CloudApi = new K3CloudApi();
        QueryParam queryParam = new QueryParam();
        queryParam.setFieldKeys("FAUXPROPID.FF100001.FDATAVALUE,FMaterialId.Fnumber");
        queryParam.setFilterString("FStockId.fnumber in (" + ckId + ") and FMaterialId.fnumber in (" + number + ")");
        queryParam.setFormId(K3FromEnum.STK_INVENTORY.getName());
        String origin = "";
        try {
            List<List<Object>> lists = k3CloudApi.executeBillQuery(JSON.toJSONString(queryParam));
            if (lists.isEmpty()) {
                log.info("金蝶接口未返回数据: {}", number);
                return new ArrayList<>();
            }
            List<OrgGoods> collect = lists.stream().map(m -> {
                OrgGoods orgGoods = new OrgGoods();
                orgGoods.setOrgName(String.valueOf(m.get(0)));
                orgGoods.setNumber(String.valueOf(m.get(1)));
                return orgGoods;
            }).collect(Collectors.toList());
            return collect;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static List<PopErpSkuStockV2Dto> convertToPopSkuStockDto(List<List<Object>> lists) {
        List<PopErpSkuStockV2Dto> collect1 = lists.stream().map(m -> {
            PopErpSkuStockV2Dto popErpSkuStockV2Dto = new PopErpSkuStockV2Dto();
            popErpSkuStockV2Dto.setErpCode(String.valueOf(m.get(0)));
            popErpSkuStockV2Dto.setAvailableQyt(BigDecimal.valueOf(Double.valueOf(String.valueOf(m.get(1)))));
            return popErpSkuStockV2Dto;
        }).collect(Collectors.toList());

        Map<String, List<PopErpSkuStockV2Dto>> collect = collect1.stream().collect(Collectors.groupingBy(PopErpSkuStockV2Dto::getErpCode));

        if (MapUtils.isEmpty(collect)) {
            return new ArrayList<>();
        }
        List<PopErpSkuStockV2Dto> stockV2Dtos = new ArrayList<>();
        for (Map.Entry<String, List<PopErpSkuStockV2Dto>> entry : collect.entrySet()) {
            stockV2Dtos.add(stockConvertFrom(entry.getKey(), entry.getValue()));
        }
        return stockV2Dtos;
    }

    private static PopErpSkuStockV2Dto stockConvertFrom(String key, List<PopErpSkuStockV2Dto> value) {
        PopErpSkuStockV2Dto popErpSkuStockDto = new PopErpSkuStockV2Dto();
        popErpSkuStockDto.setErpCode(key);
        BigDecimal qty = value.stream().map(m -> m.getAvailableQyt()).filter(Objects::nonNull).reduce(BigDecimal::add).orElse(BigDecimal.valueOf(0));
        popErpSkuStockDto.setAvailableQyt(qty);
        return popErpSkuStockDto;
    }


}
